package eu.smoothit.sis.admin.backendBean;

import java.util.ArrayList;
import java.util.List;
import javax.faces.event.ValueChangeEvent;

import eu.smoothit.sis.admin.backendBean.superclass.DataTable;
import eu.smoothit.sis.db.SisDAOFactory;
import eu.smoothit.sis.db.api.daos.IPRangeDAO;
import eu.smoothit.sis.db.impl.entities.IPRangeConfigEntry;

public class MeteringIPRangeConfigBean extends DataTable<IPRangeConfigEntry> {
	// private IPRangeConfigEntry apiQuestions;
	// define DAO
	SisDAOFactory factory = SisDAOFactory.getFactory();
	protected IPRangeDAO dataDAO = factory.createIPRangeDAO();
	boolean filterLocal;

	public boolean isFilterLocal() {
		return filterLocal;
	}

	// dataTableColumn Names for sorting TODO
	private static String idValue = "id";

	// initialization
	public MeteringIPRangeConfigBean() {
		// super(idValue);
		// apiQuestions = new IPRangeConfigEntry();
		// actionLoad();
	}

	// getter && setter
	// ----------------------------------------------------------------------------------

	public String getIdValue() {
		return idValue;
	}

	public List<IPRangeConfigEntry> getDataList() {
		refreshDataList();
		return dataList;
	}

	public void refreshDataList() {
		if (!editMode) {
			//filterLocal stand for local/remote selection
			if (!filterLocal)
				try {
					dataList = dataDAO.getAll();
				} catch (Exception e) {
					setErrorMessage(e.getMessage() + " Cause: " + e.getCause());
					e.printStackTrace();
				}
			sort();
		}
	}

	// Form actions
	// ----------------------------------------------------------------------------------

	public void actionLoad_old() {
		try {
			// show all records
			dataList = dataDAO.getAll();
		} catch (Exception e) {
			setErrorMessage(e.getMessage() + " Cause: " + e.getCause());
			e.printStackTrace();
			return;
		}
	}

	/**
	 * overload actionAdd(E) as JSF web page allows methods without parameters
	 * only.
	 */
	public void actionAdd() {
		actionAdd(new IPRangeConfigEntry());
	}

	@Override
	public void actionDelete() {
		// Get selected items.
		List<IPRangeConfigEntry> deleteItems = new ArrayList<IPRangeConfigEntry>();
		for (int index = 0; index < dataList.size(); index++) {
			if (isSelectedRow(index)) {
				deleteItems.add(dataList.get(index));
			}
		}
		if (deleteItems.isEmpty()) {
			setErrorMessage("Select at least one item to delete.");
			return;
		}
		String deletedItemsNames = "";
		try {
			for (IPRangeConfigEntry deleteItem : deleteItems) {
				dataDAO.remove(deleteItem);
				deletedItemsNames = deletedItemsNames + "(ID"
						+ deleteItem.getId() + ")";
			}
		} catch (Exception e) {
			setErrorMessage(e.getMessage() + " Cause: " + e.getCause());
			e.printStackTrace();
			return;
		}
		// Reload data and go to first page.
		// actionLoad();
		// Feedback indicating which items deleted
		setErrorMessage("Deleted entries: " + deletedItemsNames);
	}

	@Override
	public void actionSave() {
		// do not log for actionSave, as there are serialized type,which cause
		// exception
		List<IPRangeConfigEntry> editItems = new ArrayList<IPRangeConfigEntry>();
		for (int index = 0; index < dataList.size(); index++) {
			if (isEditModeRow(index)) {
				editItems.add(dataList.get(index));
			}
		}
		try {
			for (IPRangeConfigEntry editItem : editItems) {
				if (editItem.getId() == 0)
					// id=0 stand for new added entry
					dataDAO.persist(editItem);
				else
					// otherwise, it is existing entry, update it.
					dataDAO.update(editItem);
			}
		} catch (Exception e) {
			setErrorMessage(e.getMessage() + " Cause: " + e.getCause());
			e.printStackTrace();
			return;
		}
		// reload data.
		super.actionSave();
	}

	@Override
	public void actionRefresh() {
		filterLocal = false;
		super.actionRefresh();
		resetErrorMessage();
	}

	public void filtering_Listener(ValueChangeEvent event) {
		IPRangeConfigEntry Question = new IPRangeConfigEntry();
		// set local
		String localTag = (String) event.getNewValue();
		if (!localTag.equals("all")){
			filterLocal = true;
			Question.setLocal(Boolean.parseBoolean(localTag));
			try {
				dataList = dataDAO.get(Question);
			} catch (Exception e) {
				setErrorMessage(e.getMessage() + " Cause: " + e.getCause());
				e.printStackTrace();
				return;
			}
		}
		else filterLocal = false;
		
	}

}
